//Apr 7, 2023 Changed to move and send pic every 5 minutes //Apr 4, 2023 Added servo to move camera for each pic //Apr 3, 2023 Added time from internet to trigger pic capture and send email //Updates time from internet once a minute at 15 sec counter //Uses millis() 1 second counter for seconds. Rest of time from internet //Seconds counter synched to internet seconds at counter==15 in NTP time setup 3 //Note ******NTP time setup 4 and 5 of 6 not needed******* //First few pictures after reboot are dark until auto exposure corrects #include "esp_camera.h" #include "SPI.h" #include "driver/rtc_io.h" #include "ESP32_MailClient.h" #include #include #include #include #define SERVO_1 2 Servo servo1; int previousMillis = 0; // millis() and counter used for seconds not from internet int currentMillis, servobit, enable, minenable; int picbit = 1; int counter = 0; int servocounter = 0; // To send Email using Gmail use port 465 (SSL) and SMTP Server smtp.gmail.com // YOU MUST ENABLE less secure app option https://myaccount.google.com/lesssecureapps?pli=1 const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD"; #define sender_email "############@gmail.com" #define sender_email_password "############" //App password from Google #define SMTP_Server "smtp.gmail.com" #define SMTP_Server_Port 465 #define email_subject "ESP32-CAM Photo Captured" #define email_recipient "############@gmail.com" // can be the same as sender #define CAMERA_MODEL_AI_THINKER #if defined(CAMERA_MODEL_AI_THINKER) #define PWDN_GPIO_NUM 32 #define RESET_GPIO_NUM -1 #define XCLK_GPIO_NUM 0 #define SIOD_GPIO_NUM 26 #define SIOC_GPIO_NUM 27 #define Y9_GPIO_NUM 35 #define Y8_GPIO_NUM 34 #define Y7_GPIO_NUM 39 #define Y6_GPIO_NUM 36 #define Y5_GPIO_NUM 21 #define Y4_GPIO_NUM 19 #define Y3_GPIO_NUM 18 #define Y2_GPIO_NUM 5 #define VSYNC_GPIO_NUM 25 #define HREF_GPIO_NUM 23 #define PCLK_GPIO_NUM 22 #else #error "Camera model not selected" #endif SMTPData smtpData; #define IMAGE_PATH "/image.jpg" //***********NTP time intialize 1 of 6******************************************************** #include "time.h" const char* Timezone = "EST5EDT,M3.2.0,M11.1.0"; // EST USA corrects for DST String Date_str, Time_str, Time_format, Year_str, Month_str, Day_str, Hour_str, Min_str, Sec_str; int Year_integer, Month_integer, Day_integer, Hour_integer, Min_integer, Sec_integer, dow; //********** end NTP time intialize*********************************************************** void setup() { servo1.attach(SERVO_1, 500, 2500); // pin, servo PWM range for HS-422 0 to 195 degrees Serial.begin(115200); Serial.println(); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi..."); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); // WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector if (!SPIFFS.begin(true)) { Serial.println("An Error has occurred while mounting SPIFFS"); ESP.restart(); } else { delay(500); Serial.println("SPIFFS mounted successfully"); } bool formatted = SPIFFS.format(); delay(2000); // Serial.print("IP Address: http://"); // Serial.println(WiFi.localIP()); camera_config_t config; config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; config.pin_d0 = Y2_GPIO_NUM; config.pin_d1 = Y3_GPIO_NUM; config.pin_d2 = Y4_GPIO_NUM; config.pin_d3 = Y5_GPIO_NUM; config.pin_d4 = Y6_GPIO_NUM; config.pin_d5 = Y7_GPIO_NUM; config.pin_d6 = Y8_GPIO_NUM; config.pin_d7 = Y9_GPIO_NUM; config.pin_xclk = XCLK_GPIO_NUM; config.pin_pclk = PCLK_GPIO_NUM; config.pin_vsync = VSYNC_GPIO_NUM; config.pin_href = HREF_GPIO_NUM; config.pin_sscb_sda = SIOD_GPIO_NUM; config.pin_sscb_scl = SIOC_GPIO_NUM; config.pin_pwdn = PWDN_GPIO_NUM; config.pin_reset = RESET_GPIO_NUM; config.xclk_freq_hz = 20000000;//5000000 config.pixel_format = PIXFORMAT_JPEG; if(psramFound()){ config.frame_size = FRAMESIZE_UXGA; config.jpeg_quality = 10; config.fb_count = 2; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 12; config.fb_count = 1; } esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x", err); return; } //********** NTP time setup 2 of 6*********************************************** configTime(0, 0, "pool.ntp.org", "time.nist.gov"); setenv("TZ", Timezone, 1); Time_format = "I"; // or StartTime("I"); //***********end NTP time setup************************************************** captureSave_image();//take some pics after booting to let auto exposure correct for too dark delay(1000); captureSave_image(); delay(1000); captureSave_image(); delay(1000); sendImage(); // send image on boot } void loop() { if ( ((Hour_integer > 6)&&(Hour_integer < 20))&& // set enable when system is active 7 am to 8 pm and every 5 minutes ((Min_integer==0)|| (Min_integer== 5)|| (Min_integer==10)|| (Min_integer==15)|| (Min_integer==20)|| (Min_integer==25)|| (Min_integer==30)|| (Min_integer==35)|| (Min_integer==40)|| (Min_integer==45)|| (Min_integer==50)|| (Min_integer==55)) ) { // set minutes when system is active. Every 5 minutes. enable = 1; } else{ enable = 0; } currentMillis=millis(); if (currentMillis - previousMillis >= 1000){// execute the following once every second previousMillis = currentMillis; counter = counter +1; //seconds counter based on millis() not internet seconds //********** NTP time setup 3 of 6*********************************************** if ((counter == 2)) //Update time from internet once per minute { UpdateLocalTime(Time_format);// update time from internet counter = Sec_integer; // synch counter } //**********end NTP time setup 3 of 6************************************************ if (counter == 55) servobit = 1; if ((enable == 1)&&(servobit == 1)&&(counter == 59)){ servobit = 0; servocounter = servocounter +1; //update servo position once every minute if (servocounter == 5) servocounter = 0; // reset servo position to zero after 5 mins //*******Servo Move**************************************************************** if (servocounter == 0) servo1.write(0); if (servocounter == 1) servo1.write(30); if (servocounter == 2) servo1.write(60); if (servocounter == 3) servo1.write(90); if (servocounter == 4) servo1.write(120); //*******End of Servo Move********************************************************* } Serial.println("Internet Date and Time "); Serial.println(Year_integer); //year Serial.println(Month_integer);//month Serial.println(Day_integer);//day Serial.println(Hour_integer);//hour Serial.println(Min_integer);//minute Serial.println(Sec_integer);//second Serial.println(Date_str);//Date string Serial.println(Time_str);//Time string Serial.println(servocounter);//servo position counter Serial.println(counter);//1 sec counter Serial.println(enable); if (counter == 45) picbit = 1; // set bit to capture and send pic only once if (counter == 60) counter = 0; // reset to zero at 60 seconds. synched from internet at 15 seconds if ((enable == 1)&&(picbit == 1)&&(counter > 25)&&(counter < 30)) { // capture and send pic once per minute when enabled picbit = 0; captureSave_image(); sendImage(); // bool formatted = SPIFFS.format(); // delay(1000); SPIFFS.remove(IMAGE_PATH); } } } // Check if photo capture was successful bool check_photo( fs::FS &fs ) { File f_pic = fs.open( IMAGE_PATH ); unsigned int pic_sz = f_pic.size(); return ( pic_sz > 100 ); } // Capture Photo and Save it to SPIFFS void captureSave_image( void ) { camera_fb_t * fb = NULL; bool ok = 0; do { Serial.println("ESP32-CAM capturing photo..."); fb = esp_camera_fb_get(); delay(500); if (!fb) { Serial.println("Failed"); return; } Serial.printf("Picture file name: %s\n", IMAGE_PATH); File file = SPIFFS.open(IMAGE_PATH, FILE_WRITE); if (!file) { Serial.println("Failed to open file in writing mode"); } else { file.write(fb->buf, fb->len); Serial.print("The picture has been saved in "); Serial.print(IMAGE_PATH); Serial.print(" - Size: "); Serial.print(file.size()); Serial.println(" bytes"); } file.close(); esp_camera_fb_return(fb); ok = check_photo(SPIFFS); } while ( !ok ); } void sendImage( void ) { smtpData.setLogin(SMTP_Server, SMTP_Server_Port, sender_email, sender_email_password); smtpData.setSender("ESP32-CAM", sender_email); smtpData.setPriority("High"); smtpData.setSubject(email_subject); smtpData.setMessage("CAM-3 Captured Image", false);// added 2 to id which cam smtpData.addRecipient(email_recipient); smtpData.addAttachFile(IMAGE_PATH, "image/jpg"); smtpData.setFileStorageType(MailClientStorageType::SPIFFS); smtpData.setSendCallback(sendCallback); if (!MailClient.sendMail(smtpData)) Serial.println("Error sending Email, " + MailClient.smtpErrorReason()); smtpData.empty(); } void sendCallback(SendStatus msg) { Serial.println(msg.info()); } //******NTP time setup 6 of 6 *********************************************************************** void UpdateLocalTime(String Format){ time_t now; time(&now); //See http://www.cplusplus.com/reference/ctime/strftime/ char hour_output[30], day_output[30]; strftime(day_output, 30, "%a %m-%d-%y", localtime(&now)); // Formats date as: Sat Jun-24-17 strftime(hour_output, 30, "%T", localtime(&now)); // Formats time as: 2:05:49pm Date_str = day_output; Time_str = hour_output; //*** get individual date and time numbers strftime(day_output, 30, "%Y", localtime(&now)); Year_str = day_output; Year_integer = Year_str.toInt(); strftime(day_output, 30, "%m", localtime(&now)); Month_str = day_output; Month_integer = Month_str.toInt(); strftime(day_output, 30, "%e", localtime(&now)); Day_str = day_output; Day_integer = Day_str.toInt(); strftime(hour_output, 30, "%H", localtime(&now)); Hour_str = hour_output; Hour_integer = Hour_str.toInt(); strftime(hour_output, 30, "%M", localtime(&now)); Min_str = hour_output; Min_integer = Min_str.toInt(); strftime(hour_output, 30, "%S", localtime(&now)); Sec_str = hour_output; Sec_integer = Sec_str.toInt(); } //*****end of NTP time setup 6 of 6**************************************************